package com.timemanage.activity

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import com.timemanage.R
import com.timemanage.dao.AppDatabase
import com.timemanage.dao.UserDao
import com.timemanage.entity.User
import com.timemanage.entity.Work
import com.timemanage.utils.MyApplication
import kotlinx.android.synthetic.main.work_add.view.*
import kotlinx.android.synthetic.main.work_detail.view.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.thread

class detailAdapter(val context: Context,val sort_id:ArrayList<String>,val sort_name:ArrayList<String>,
                    val sort_body:ArrayList<String>,val start_Time:ArrayList<String>,
                    val end_Time:ArrayList<String>):RecyclerView.Adapter<detailAdapter.MyViewHolder>() {
    inner class MyViewHolder(view:View):RecyclerView.ViewHolder(view){
        val sortName: TextView = itemView.findViewById(R.id.sort_name_text)
        val sortBody: TextView = itemView.findViewById(R.id.sort_body_text)
        val startTime : TextView = itemView.findViewById(R.id.starttime_text)
        val endTime : TextView = itemView.findViewById(R.id.endtime_text)
        val mainLayout: LinearLayout = itemView.findViewById(R.id.mainLayout)
    }
    var mcontext :SortDetail = context as SortDetail
    var work_Id = ""
    var work_Name = ""
    var work_Body = ""
    var work_Start = ""
    var work_End = ""
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.row_item1,parent,false
        )
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val sdf = SimpleDateFormat("yyyy/MM/dd hh:mm a")
        //"yyyy/MM/dd hh:mm a"
        var Dateend=" "
        var Datestart=" "
        if(start_Time[position]!="no" && end_Time[position]!="no") {
            Dateend = sdf.format(Date(end_Time[position].toLong()))
            Datestart = sdf.format(Date(start_Time[position].toLong()))
        }
        work_Name = sort_name[position]
        work_Body = sort_body[position]
        work_Start = Datestart
        work_End = Dateend
        work_Id = sort_id[position]
        holder.sortName.text = sort_name[position]
        holder.sortBody.text = sort_body[position]
        holder.startTime.text = Datestart
        holder.endTime.text = Dateend
        holder.mainLayout.setOnClickListener {
            if(Dateend==" "&&Datestart ==" ") {
                val intent = Intent(context, SortDetail::class.java)
                intent.putExtra("ID", sort_id[position])
                intent.putExtra("title", sort_name[position])
                context.startActivity(intent)
            }
            else{
                XPopup.Builder(MyApplication.context)
                    .asCustom(WorkDetail())
                    .show();
            }
        }
    }

    override fun getItemCount() = sort_name.size
    inner class WorkDetail: CenterPopupView(context){
        override fun getImplLayoutId(): Int {
            return R.layout.work_detail
        }
        override fun onCreate(){
            super.onCreate()
            work_title.text = work_Name
            work_body.text = work_Body
            startTime.text = work_Start
            endTime.text = work_End
            floatingActionButtoncommit.setOnClickListener {
                dismiss()
            }
            floatingActionButtondelete.setOnClickListener{
                var database = AppDatabase.getDatabase(MyApplication.context)
                var userdao: UserDao = database.userDao();
                var workDao = database.WorkDao()
                val userid = userdao.selectAcUser().userId
                workDao.deleteUserWork(userid,work_Id.toInt())
                mcontext.refresh()
                dismiss()
            }
        }
    }
}